<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contacts extends CI_Controller {

	function __construct() {

		parent::__construct();

		$this->load->model('t_contacts');

		$this->load->library(array('pagination'));

	}

	public function index()
	{	
		echo 'welcome calls secret app';
	}

	public function get_total_row($user_id = -1){


	}


/**
 * [pagination description] pagination contact
 * @param  integer $index_page : index of page want to get contact
 * @param  integer $number     : number per page
 * @return json $data          : list contact of page
 */
	public function getall(){

		$data = array();

		if($this->input->post()){

			$post = $this->input->post();

			if(!empty($post[CONTACT_USER_ID])){

				$user_id = $post[CONTACT_USER_ID];
			}else{

				$data[SUCCESS] = 0;

				$data[LOG] = 'Missing user_id';
			}

			if(!empty($post[PAGE])){

				$page = $post[PAGE];
			}else{

				$page = 0;
			}

			if(!empty($post[NUMBER_PER_PAGE])){

				$number_per_page = $post[NUMBER_PER_PAGE];
			}else{

				$number_per_page = 100;
			}

			if(isset($data[SUCCESS])){

				echo json_encode($data); exit();
			}

			$rows = $this->t_contacts->get_total_rows($user_id);

			if(empty($rows)){

				$data[SUCCESS] = 0;

				$data[LOG] = 'No have contacts by user id';
			}elseif ($rows == -1) {
				
				$data[SUCCESS] = 0;

				$data[LOG] = 'user id is null';
			}else{

				$rows = ((int) ($rows/$number_per_page)) + 1;

				if($rows <= $page){

					$data[SUCCESS] = 0;

					$data[LOG] = 'page > total page';

				}else{

					$data[DATA][TOTAL_PAGES] = $rows;
				}

			}

			if(isset($data[SUCCESS])){

				echo json_encode($data); exit();
			}

			$query = $this->t_contacts->pagination($user_id, $number_per_page, $page);

			if(empty($query)){

				$data[SUCCESS] = 0;

				$data[LOG] = 'user_id is null';

			}elseif($query == -1){

				$data[SUCCESS] = 0;

				$data[LOG] = 'No have contacts by user id';
			}else{

				
			}

			$data[SUCCESS] = 1;

			$data[DATA][LISTS] = $query;

			echo json_encode($data); exit();

		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'Value post is null';
		}
	}

	/**
 * [synchronize contacts from client to server]
 * @exit() integer id contact
 */
	public function upload(){

		$data = array();

		if($this->input->post()){

			// get post data
			$contact = $this->input->post();

			// contact_id == null
			if(empty($contact[CONTACT_ID])){

				$data[SUCCESS] = 0;

				$data[LOG][CONTACT_ID] = CONTACT_ID . ' is null';
			}else{
			//contact_id != null
				$contact_id = $contact[CONTACT_ID];
			}

			// user_id == null
			if(empty($contact[CONTACT_USER_ID])){

				$data[SUCCESS] = 0;

				$data[LOG][CONTACT_USER_ID] = CONTACT_USER_ID . ' is null';
			}else{

				$user_id = $contact[CONTACT_USER_ID];
			}

			//contact name
			if(empty($contact[CONTACT_CONTACT_NAME])){

				$data[SUCCESS] = 0;

				$data[LOG][CONTACT_CONTACT_NAME] = CONTACT_CONTACT_NAME . ' is null';
			}else{

				$contact_name = $contact[CONTACT_CONTACT_NAME];
			}

			//contact numberphone
			if(empty($contact[CONTACT_PHONE_NUMBER])){

				$data[SUCCESS] = 0;

				$data[LOG][CONTACT_PHONE_NUMBER] = CONTACT_PHONE_NUMBER . ' is null';
			}else{

				$phone_number = $contact[CONTACT_PHONE_NUMBER];
			}

			// check data[SUCCESS]
			if(isset($data[SUCCESS])){

				echo json_encode($data); exit();
			}

			// contact_id == -1 : new contact or install app
			if($contact_id == -1){

				$query_contact = $this->t_contacts->get_data_by_property(CONTACT_ID, array(CONTACT_CONTACT_NAME => $contact_name, CONTACT_PHONE_NUMBER => $phone_number, CONTACT_USER_ID => $user_id));

				// if $query_contact empty then insert else get contact_id
				if(empty($query_contact)){

					$insert_contact = $this->t_contacts->set_data(array(CONTACT_CONTACT_NAME => $contact_name,  CONTACT_PHONE_NUMBER => $phone_number, CONTACT_USER_ID => $user_id));

					if(!empty($insert_contact)){

						$data[SUCCESS] = 1;

						$data[DATA] = $insert_contact;
					}else{

						$data[SUCCESS] = 0;

						$data[LOG]['insert'] = 'Insert false';
					}
				}else{

					$data[SUCCESS] = 1;

					$data[DATA] = $query_contact[0][CONTACT_ID];
				}

			}else{
			// update contact by CONTACT_ID

				$query = $this->t_contacts->get_data_by_id($contact_id);

				if(empty($query)){

					$data[SUCCESS] = 0;

					$data[LOG]['update'] = 'No have contact id';
					
				}else{

					$query_update = $this->t_contacts->update_data_by_id(array(CONTACT_CONTACT_NAME => $contact_name, CONTACT_PHONE_NUMBER => $phone_number), $contact_id);

					if($query_update){

						$data[SUCCESS] = 1;

						$data[DATA] = $contact_id;
					}else{

						$data[SUCCESS] = 0;

						$data[LOG]['update'] = 'No row affected';
					}	
				}
			}

			echo json_encode($data); exit();
		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'Value post is null';
		}

		echo json_encode($data); exit();
	}

/**
 * [get_all_contacts description]
 * @return [type] [description]
 * 

	public function getall(){

		$data = array();

		if($this->input->post()){

			$post = $this->input->post();

			if(!empty($post[CONTACT_USER_ID])){

				$user_id = $post[CONTACT_USER_ID];
			}else{

				$data[SUCCESS] = 0;

				$data[LOG][CONTACT_USER_ID] = 'user_id is null';
			}

			if(isset($data[SUCCESS])){

				echo json_encode($data); exit();
			}

			$query = $this->t_contacts->get_data_by_property(array(CONTACT_USER_ID => $user_id));

			if(empty($query)){

				$data[SUCCESS] = 0;

				$data[LOG][CONTACT_USER_ID] = 'No have contact by user_id';
			}else{

				$data[SUCCESS] = 1;

				$data[DATA] = $query;
			}

			echo json_encode($data); exit();

		}else{

			$data[SUCCESS] = 0;

			$data[LOG] = 'Value post is null';
		}

		echo json_encode($data); exit();
	}
*/
}